package com.shyroke.util;

import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.ConvertExpert;
import entity.Expert;

public class ConvertUtil {

	public static List<ConvertExpert> covert(Map<Integer, Expert> expertMap) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		
		if(expertMap == null ||expertMap.size()==0 ) {
			return null;
		}
		
		List<ConvertExpert> convertExpertList = new ArrayList<>();
		ConvertExpert convertExpert = null;
		for(int i=0;i<expertMap.size(); i++) {
			Expert expert = expertMap.get(i+1);
			 convertExpert = ConvertUtil.convertByExpert(expert);
			 convertExpertList.add(convertExpert);
		}
		
		return convertExpertList;
	}

	private static ConvertExpert convertByExpert(Expert expert) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ConvertExpert convertExpert = new ConvertExpert();
		convertExpert.setId(expert.getId());
		Field[] fields = expert.getClass().getDeclaredFields();
		Field[] convertExpertFields = convertExpert.getClass().getDeclaredFields();
		for(int i=1;i<fields.length;i++) {
			String name = fields[i].getName(); //获取属性的名字
			String methodName = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写，方便构造get，set方法
			 Method m = expert.getClass().getMethod("get"+methodName);
			 String value = (String)m.invoke(expert);
			 List<Double> valueList = StringUtil.convertList(value); 
			 Map<String,List<Double>> paramMap = null;
			 for(int j=0;j<convertExpertFields.length;j++) {
				 String convertName = convertExpertFields[j].getName();
				 String methodName1 = convertName.substring(0,1).toUpperCase()+convertName.substring(1); 
				 if(convertName.contains(name)) {
					 Method m2 = convertExpert.getClass().getMethod("set"+methodName1,Map.class);
					 paramMap = new HashMap<>();
					 paramMap.put(name, valueList);
					 m2.invoke(convertExpert, paramMap);
					 break;
				 }
				 
			 }
		
		}
		
		return convertExpert;
	}

	public static Map<String, Double> convertExpertWeight(Map<String, Double> expertImportance) {
		Map<String, Double> resultMap = new HashMap<>();
		
		for(int i=1;i<=expertImportance.size();i++) {
			resultMap.put("e"+i, expertImportance.get(i+""));
		}
		
		return resultMap;
	}
	
	
	
}
